{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conditionals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Testing truth value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `bool`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('type of True and False: {}'.format(type(True)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('0: {}, 1: {}'.format(bool(0), bool(1)))\n",
    "print('empty list: {}, list with values: {}'.format(bool([]), bool(['woop'])))\n",
    "print('empty dict: {}, dict with values: {}'.format(bool({}), bool({'Python': 'cool'})))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `==, !=, >, <, >=, <=`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('1 == 0: {}'.format(1 == 0))\n",
    "print('1 != 0: {}'.format(1 != 0))\n",
    "print('1 > 0: {}'.format(1 > 0))\n",
    "print('1 > 1: {}'.format(1 > 1))\n",
    "print('1 < 0: {}'.format(1 < 0))\n",
    "print('1 < 1: {}'.format(1 < 1))\n",
    "print('1 >= 0: {}'.format(1 >= 0))\n",
    "print('1 >= 1: {}'.format(1 >= 1))\n",
    "print('1 <= 0: {}'.format(1 <= 0))\n",
    "print('1 <= 1: {}'.format(1 <= 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can combine these:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('1 <= 2 <= 3: {}'.format(1 <= 2 <= 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `and, or, not`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_is_cool = True\n",
    "java_is_cool = False\n",
    "empty_list = []\n",
    "secret_value = 3.14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Python and java are both cool: {}'.format(python_is_cool and java_is_cool))\n",
    "print('secret_value and python_is_cool: {}'.format(secret_value and python_is_cool))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Python or java is cool: {}'.format(python_is_cool or java_is_cool))\n",
    "print('1 >= 1.1 or 2 < float(\"1.4\"): {}'.format(1 >= 1.1 or 2 < float('1.4')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Java is not cool: {}'.format(not java_is_cool))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can combine multiple statements, execution order is from left to right. You can control the execution order by using brackets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(bool(not java_is_cool or secret_value and  python_is_cool or empty_list))\n",
    "print(bool(not (java_is_cool or secret_value and  python_is_cool or empty_list)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `if`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "statement = True\n",
    "if statement:\n",
    "    print('statement is True')\n",
    "    \n",
    "if not statement:\n",
    "    print('statement is not True')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "empty_list = []\n",
    "# With if and elif, conversion to `bool` is implicit\n",
    "if empty_list:\n",
    "    print('empty list will not evaluate to True')  # this won't be executed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val = 3\n",
    "if 0 <= val < 1 or val == 3:\n",
    "    print('Value is positive and less than one or value is three')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `if-else`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_dict = {}\n",
    "if my_dict:\n",
    "    print('there is something in my dict')\n",
    "else:\n",
    "    print('my dict is empty :(')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `if-elif-else`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val = 88\n",
    "if val >= 100:\n",
    "    print('value is equal or greater than 100')\n",
    "elif val > 10:\n",
    "    print('value is greater than 10 but less than 100')\n",
    "else:\n",
    "    print('value is equal or less than 10')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can have as many `elif` statements as you need. In addition, `else` at the end is not mandatory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "greeting = 'Hello fellow Pythonista!'\n",
    "language = 'Italian'\n",
    "\n",
    "if language == 'Swedish':\n",
    "    greeting = 'Hejsan!'\n",
    "elif language == 'Finnish':\n",
    "    greeting = 'Latua perkele!'\n",
    "elif language == 'Spanish':\n",
    "    greeting = 'Hola!'\n",
    "elif language == 'German':\n",
    "    greeting = 'Guten Tag!'\n",
    "    \n",
    "print(greeting)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For more detailed overview about conditionals, check this [tutorial from Real Python](https://realpython.com/python-conditional-statements/)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
